﻿
Partial Class WebForm_ManageCatagory_OpenCatagoryMapping
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack() Then
            If Me.Request.QueryString("CatagoryLevel") IsNot Nothing Then
                Me.CatagoryLevel = Me.Request.QueryString("CatagoryLevel").ToString()
            End If
            Me.InitDataAndShow()
            Me.BindHierarchy()
        End If
    End Sub

    Protected Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0
        sql.AppendFormat("delete from CodeCatagoryItem ")
        sql.AppendFormat("where HeaderID = '{0}' ", Me.HeaderID)
        sql.AppendFormat("and LevelName= '{0}' ", Me.LevelName)
        sql.AppendFormat("and HeaderCode= '{0}' ", Me.HeaderCode)
        For Each node As TreeNode In tvHierarchy.CheckedNodes
            If node.Text <> Me.LevelName Then
                sql.AppendFormat(";insert into CodeCatagoryItem(HeaderID,HeaderCode,ItemCode,LevelName,CreateBy,CreateDate) ")
                sql.AppendFormat("Select ")
                sql.AppendFormat("'{0}' ", Me.HeaderID)
                sql.AppendFormat(",'{0}' ", Me.HeaderCode)
                sql.AppendFormat(",'{0}' ", node.Value)
                sql.AppendFormat(",'{0}' ", Me.LevelName)
                sql.AppendFormat(",'{0}' ", clsAuth.GetUserID())
                sql.AppendFormat(",Getdate() ")
                sql.AppendFormat("; ")
            End If
        Next

        dat.AccData(sql.ToString())
        MyObj.ShowMessage("Apply Successful!")
    End Sub

#Region "方法"
    Public Sub InitDataAndShow()
        Me.HeaderID = Me.Request.QueryString("HeaderID")
        Me.HeaderCode = Me.Request.QueryString("HeaderCode")
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        sql.AppendFormat("Select Division from CodeCatagoryHeader ")
        sql.AppendFormat("where HeaderID = '{0}' ", HeaderID)

        Me.Division = dat.GetData(sql).Rows(0)("Division").ToString()
    End Sub
#End Region

#Region "繫結資料"

    Private Sub BindHierarchy()
        Dim MyDT As DataTable
        Dim HiDT As Data.DataTable
        Me.tvHierarchy.Attributes.Add("onClick", "OnCheckEvent()")
        Me.tvHierarchy.Visible = True
        Me.tvHierarchy.Nodes.Clear()

        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        '取出有選擇的Item
        sql.AppendFormat("Select Description,ItemCode from CodeCatagoryView ")
        sql.AppendFormat("where HeaderID = '{0}' ", Me.HeaderID)
        sql.AppendFormat("and Division= '{0}' ", Me.Division)
        sql.AppendFormat("and HeaderCode= '{0}' ", Me.HeaderCode)
        HiDT = dat.GetData(sql)
        Me.lblDivisionValue.Text = Me.Division
        Me.lblDescriptionValue.Text = Me.Description

        Dim lManager As New LevelManager
        Dim LevelItem As DataRow = lManager.GetLevelName2(Me.Division, "SF", Me.CatagoryLevel)
        Me.LevelName = LevelItem("LevelName").ToString()

        Dim node As New TreeNode
        node.Text = Me.LevelName
        node.Value = ""
        Me.tvHierarchy.Nodes.Add(node)

        '取出Hierarchy
        sql.Length = 0
        sql.AppendFormat("select a.Description ")
        If LevelItem("Chain") = "Y" Then
            sql.AppendFormat(",substring(a.Code,1,{0}*2) as Code ", LevelItem("Sequence"))
        Else
            sql.AppendFormat(",substring(a.Code,{0}*2-1,2) as Code ", LevelItem("Sequence"))
        End If
        sql.AppendFormat("from ProductHierarchyOriginal a  ")
        sql.AppendFormat("where a.LevelName='{0}' ", Me.LevelName)
        sql.AppendFormat("and Left(a.Code,2) ='{0}' ", Me.HeaderCode)
        'sql.AppendFormat("order by Left(a.Code,2) ")
        If LevelItem("Chain") = "Y" Then
            sql.AppendFormat("order by substring(a.Code,1,{0}*2) ", LevelItem("Sequence"))
        Else
            sql.AppendFormat("order by substring(a.Code,{0}*2-1,2) ", LevelItem("Sequence"))
        End If
        'MyObj.WriteLog(sql.ToString())
        MyDT = dat.GetData(sql)

        Dim MyRow As DataRow()
        For i As Integer = 0 To MyDT.Rows.Count - 1
            Dim hnode As New TreeNode
            hnode.Value = MyDT.Rows(i)("Code").ToString()
            'hnode.Text = "(" & Right(hnode.Value, 2) & ")" & MyDT.Rows(i)("Description").ToString()
            hnode.Text = "(" & hnode.Value & ")" & MyDT.Rows(i)("Description").ToString()
            MyRow = HiDT.Select(String.Format("ItemCode='{0}'", hnode.Value))

            If MyRow.Length > 0 Then
                hnode.Checked = True
            End If
            Me.tvHierarchy.Nodes(0).ChildNodes.Add(hnode)
        Next

    End Sub
#End Region

#Region "Property"
    Public Property HeaderID() As String
        Get
            Return ViewState("HeaderID")
        End Get
        Set(ByVal value As String)
            ViewState("HeaderID") = value
        End Set
    End Property

    Public Property HeaderCode() As String
        Get
            Return ViewState("HeaderCode")
        End Get
        Set(ByVal value As String)
            ViewState("HeaderCode") = value
        End Set
    End Property

    Public Property Division() As String
        Get
            Return ViewState("Division")
        End Get
        Set(ByVal value As String)
            ViewState("Division") = value
        End Set
    End Property

    Public Property LevelName() As String
        Get
            Return ViewState("LevelName")
        End Get
        Set(ByVal value As String)
            ViewState("LevelName") = value
        End Set
    End Property

    Public Property Description() As String
        Get
            If ViewState("Description") Is Nothing Then
                ViewState("Description") = Session("Description" & Me.HeaderID)
            End If
            Return ViewState("Description")
        End Get
        Set(ByVal value As String)
            ViewState("Description") = value
        End Set
    End Property

    Public Property CatagoryLevel() As String
        Get
            Return ViewState("CatagoryLevel")
        End Get
        Set(ByVal value As String)
            ViewState("CatagoryLevel") = value
        End Set
    End Property
#End Region


End Class
